package com.sxfq.loansupermarket.backsystem.modular.arrival.service.impl;

import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsService;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerPublicService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class CustomerPublicServiceImpl implements ICustomerPublicService {
    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    IReservationsService reservationsService;

    public String getCustomerStatus(CustomerInfo customerInfo) {
        String sql1 = " select id from " + ShiroKit.getUser().getCustomer_info() + " a where a.reservations_id='" + customerInfo.getReservationsId() + "' " +
                "and (a.arrival_status!=1 or a.arrival_status is null or a.arrival_status ='' or a.arrival_status!=8 or a.arrival_status!=9 );";
        List<CustomerInfo> list1 = sqlMapper.selectList(sql1, CustomerInfo.class);
        if (list1.size() > 0) {
            String sql2 = "SELECT id,COUNT(CASE a.isdeal_status WHEN '已成交' THEN 0 END) AS consumption " +
                    "from " + ShiroKit.getUser().getCustomer_info() + " a where a.reservations_id='" + customerInfo.getReservationsId() + "' " +
                    "group by a.reservations_id HAVING consumption>0";
            List<CustomerInfo> list2 = sqlMapper.selectList(sql2, CustomerInfo.class);
            if (list2.size() > 0) {
                return "复诊";
            } else {
                return "新客多次";
            }
        } else {
            return "新客首次";
        }
    }

    public String getStartTime() {
        //设置开始时间
        String starttime = DateTimeKit.now();
        String time = starttime.substring(0, 11);
        int hour = Integer.parseInt(starttime.substring(11, 13));
        int minute = Integer.parseInt(starttime.substring(14, 16));
        minute = (minute / 15) * 15;
        String minutes = minute + "";
        if ("0".equals(minutes)) {
            minutes = "00";
        }
        starttime = time + hour + ":" + minutes + ":00";
        return starttime;
    }

    public String getEndTime() {
        //设置结束时间
        String starttime = DateTimeKit.now();
        String time = starttime.substring(0, 11);
        int hour = Integer.parseInt(starttime.substring(11, 13));
        int minute = Integer.parseInt(starttime.substring(14, 16));
        minute = (minute / 15) * 15;
        String minutes = minute + "";
        if ("0".equals(minutes)) {
            minutes = "00";
        }
        String endtime = "";
        if (hour + 1 == 24) {
            time = time.substring(0, 9) + (Integer.parseInt(time.substring(9, 10)) + 1) + " ";
            endtime = time + "00" + ":" + minutes + ":00";
        } else {
            endtime = time + (hour + 1) + ":" + minutes + ":00";
        }
        return endtime;
    }

    public String getMaxYellowNumber() {
        String sql2 = " select id,max(yellow_number) as yellowNumber from " + ShiroKit.getUser().getCustomer_info() + " limit 1;";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql2, CustomerInfo.class);
        //设置黄页编号     //设置黄页编号
        String yellowNumbers = "0000123";
        return yellowNumbers + "";
    }

    //判断今天是否来过店
    public int isComeToday(String customerId) {
        String sql = "select id from " + ShiroKit.getUser().getCustomer_info() + " a where a.reservations_id=(select reservations_id " +
                "from " + ShiroKit.getUser().getCustomer_info() + " a where a.id='" + customerId + "') " +
                " and left(a.arrival_time,10)=left(now(),10) limit 1;";
        List<CustomerInfo> list = sqlMapper.selectList(sql, CustomerInfo.class);
        return list.size();
    }

    //点重咨，判断之前来店是否有成交，有为老客，从来没有为新客多次
    public String getOldOrReconsultingStatus(String reservationsId) {
        String sql = "select id from distinction_customer_status where " +
                " reservations_id='" + reservationsId + "' and deal_status='已成交' and left(update_time,10)<left(now(),10) and status=1 ; ";
        List<CustomerInfo> list = sqlMapper.selectList(sql, CustomerInfo.class);
        if (list.size() > 0) {
            return "老客";
        } else {
            return "新客多次";
        }
    }

    //患者第二次到店，判断是今天到店的还是以前来过，今天到店多次客户状态都是新客首次，
    public String getNewCustomerStatus(String reservationsId) {
        String sql = "select id from distinction_customer_status where " +
                " reservations_id='" + reservationsId + "' and left(add_time,10)<left(now(),10) and status=1 ; ";
        List<CustomerInfo> list = sqlMapper.selectList(sql, CustomerInfo.class);
        if (list.size() > 0) {
            return getOldOrReconsultingStatus(reservationsId);
        } else {
            return "新客首次";
        }
    }

    //年龄获得头像
    public String getSexPt(CustomerInfo info) {
        Integer customerAge=info.getCustomerAge();
        Integer sex=info.getCustomerSex();
        if (StringUtil.isEmpty(customerAge)) {
            return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
        }else if (customerAge >= 0 && customerAge < 18) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        } else if (customerAge  >= 18 && customerAge  <= 50) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        } else if (customerAge  > 50) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        }else{
            return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
        }
    }

    public List<CustomerInfo> getUpdatePhoneCount(String customerPhone, String id) {
        String sql = "select id, customer_phone,reservations_id,medical_record_number from " + ShiroKit.getUser().getCustomer_info() + " where id !='" + id + "' and customer_phone='" + customerPhone + "' and phone_shar_ship='';";
        List<CustomerInfo> infoList = sqlMapper.selectList(sql, CustomerInfo.class);
        return infoList;
    }
}